package org.zakariya.stickyheadersapp.tool;

import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;
import org.zakariya.stickyheaders.model.ItemModel;
import org.zakariya.stickyheadersapp.ResourceTable;

import java.util.List;
/**
 * TopToolUtils
 *
 * @since 2021-08-20
 */
public class TopToolUtils {
    private static int selectPosition;

    public static void showScrollTool(Context context, ListContainer listContainer) {
        if (listContainer == null) {
            System.out.println("listContainer == null");
            return;
        }
        CommonDialog commonDialog = new CommonDialog(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_scroll, null, false);
        RadioContainer radio_container = (RadioContainer) component.findComponentById(ResourceTable.Id_radio_container);
        Text smooth = (Text) component.findComponentById(ResourceTable.Id_smooth);
        Text jump = (Text) component.findComponentById(ResourceTable.Id_jump);
        selectPosition = 0;
        radio_container.setMarkChangedListener((radioContainer, index) -> selectPosition = index);

        smooth.setClickedListener(component1 -> {
            commonDialog.remove();
            switch (selectPosition) {
                case 0:
                    listContainer.scrollTo(0);
                    break;
                case 1:
                    listContainer.scrollTo(listContainer.getChildCount() / 2);
                    break;
                case 2:
                    listContainer.scrollTo(listContainer.getChildCount() - 1);
                    break;
                default:
                    break;
            }
        });

        jump.setClickedListener(component12 -> {
            commonDialog.remove();
            switch (selectPosition) {
                case 0:
                    listContainer.scrollTo(0);
                    break;
                case 1:
                    listContainer.scrollTo(listContainer.getChildCount() / 2);
                    break;
                case 2:
                    listContainer.scrollTo(listContainer.getChildCount() - 1);
                    break;
                default:
                    break;
            }
        });
        commonDialog.setContentCustomComponent(component);
        commonDialog.setAutoClosable(true);
        commonDialog.setAlignment(LayoutAlignment.HORIZONTAL_CENTER);
        commonDialog.setSize(AttrHelper.vp2px(300, context), AttrHelper.vp2px(230, context));
        commonDialog.show();
    }

    public static void showViewToolNoFloat(Context context, ListContainer listContainer, List<ItemModel> dataList) {
        if (listContainer == null || dataList == null) {
            System.out.println("listContainer == null || dataList == null");
            return;
        }
        CommonDialog commonDialog = new CommonDialog(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_view_list, null, false);
        Text ok = (Text) component.findComponentById(ResourceTable.Id_ok);
        DirectionalLayout view_header = (DirectionalLayout) component.findComponentById(ResourceTable.Id_view_header);
        DirectionalLayout view_footer = (DirectionalLayout) component.findComponentById(ResourceTable.Id_view_footer);
        DirectionalLayout view_item = (DirectionalLayout) component.findComponentById(ResourceTable.Id_view_item);

        Text tv_view_header = (Text) component.findComponentById(ResourceTable.Id_tv_view_header);
        Text tv_view_item = (Text) component.findComponentById(ResourceTable.Id_tv_view_item);
        Text tv_view_footer = (Text) component.findComponentById(ResourceTable.Id_tv_view_footer);

        int firstViewIndex = listContainer.getItemPosByVisibleIndex(0);
        int visibleViewCount = listContainer.getVisibleIndexCount();
        ItemModel firstItemModel = dataList.get(firstViewIndex);
        if (firstItemModel.isHeader()) {//第一条为header
            view_header.setVisibility(Component.VISIBLE);
            view_item.setVisibility(Component.VISIBLE);
            tv_view_header.setText(firstItemModel.getIndex() + "");
            tv_view_item.setText(firstItemModel.getIndex() + 2 + "");
            for (int i = firstViewIndex; i < (visibleViewCount + firstViewIndex); i++) {
                ItemModel model = dataList.get(i);
                if (model.isEnd()) {
                    view_footer.setVisibility(Component.VISIBLE);
                    tv_view_footer.setText((i + Integer.parseInt(model.getTitle()) + 1) + "");
                    break;
                }
            }
        } else if (firstItemModel.isEnd()) {//第一条为footer
            view_footer.setVisibility(Component.VISIBLE);
            view_header.setVisibility(Component.VISIBLE);
            view_item.setVisibility(Component.VISIBLE);
            tv_view_footer.setText((firstViewIndex + Integer.parseInt(firstItemModel.getTitle()) + 1) + "");
            tv_view_header.setText((firstViewIndex + Integer.parseInt(firstItemModel.getTitle()) + 2) + "");
            tv_view_item.setText((firstViewIndex + Integer.parseInt(firstItemModel.getTitle()) + 4) + "");
        } else {//第一条为body
            view_item.setVisibility(Component.VISIBLE);
            tv_view_item.setText((firstItemModel.getIndex() + firstViewIndex + 1) + "");
            for (int i = firstViewIndex; i < (visibleViewCount + firstViewIndex); i++) {
                ItemModel model = dataList.get(i);
                if (model.isEnd()) {
                    view_footer.setVisibility(Component.VISIBLE);
                    tv_view_footer.setText((i + Integer.parseInt(model.getTitle()) + 1) + "");
                    break;
                }
            }

            for (int i = firstViewIndex; i < (visibleViewCount + firstViewIndex); i++) {
                ItemModel model = dataList.get(i);
                if (model.isHeader()) {
                    view_header.setVisibility(Component.VISIBLE);
                    tv_view_header.setText((i + Integer.parseInt(model.getTitle())) + "");
                    break;
                }
            }
        }

        ok.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                commonDialog.remove();
            }
        });

        commonDialog.setContentCustomComponent(component);
        commonDialog.setAutoClosable(true);
        commonDialog.setAlignment(LayoutAlignment.HORIZONTAL_CENTER);
        commonDialog.setSize(AttrHelper.vp2px(300, context), AttrHelper.vp2px(165, context));
        commonDialog.show();
    }

    public static void showViewTool(Context context, ListContainer listContainer, List<ItemModel> dataList) {
        if (listContainer == null || dataList == null) {
            System.out.println("listContainer == null || dataList == null");
            return;
        }
        CommonDialog commonDialog = new CommonDialog(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_view_list, null, false);
        Text ok = (Text) component.findComponentById(ResourceTable.Id_ok);
        DirectionalLayout view_header = (DirectionalLayout) component.findComponentById(ResourceTable.Id_view_header);
        DirectionalLayout view_footer = (DirectionalLayout) component.findComponentById(ResourceTable.Id_view_footer);
        DirectionalLayout view_item = (DirectionalLayout) component.findComponentById(ResourceTable.Id_view_item);

        Text tv_view_header = (Text) component.findComponentById(ResourceTable.Id_tv_view_header);
        Text tv_view_item = (Text) component.findComponentById(ResourceTable.Id_tv_view_item);
        Text tv_view_footer = (Text) component.findComponentById(ResourceTable.Id_tv_view_footer);

        try {
            int firstViewIndex = listContainer.getItemPosByVisibleIndex(0);
            int visibleViewCount = listContainer.getVisibleIndexCount();
            ItemModel firstItemModel = dataList.get(firstViewIndex);
            if (firstItemModel.isHeader()) {//第一条为header
                view_header.setVisibility(Component.VISIBLE);
                tv_view_header.setText(firstItemModel.getIndex() + "");
                for (int i = firstViewIndex; i < (visibleViewCount + firstViewIndex); i++) {
                    ItemModel model = dataList.get(i);
                    if (!model.isEnd() && !model.isHeader()) {
                        view_item.setVisibility(Component.VISIBLE);
                        tv_view_item.setText(firstItemModel.getIndex() + 2 + "");
                        break;
                    }
                }

                for (int i = firstViewIndex; i < (visibleViewCount + firstViewIndex); i++) {
                    ItemModel model = dataList.get(i);
                    if (model.isEnd()) {
                        view_footer.setVisibility(Component.VISIBLE);
                        tv_view_footer.setText(model.getIndex() + "");
                        break;
                    }
                }
            } else {
                view_header.setVisibility(Component.VISIBLE);
                ItemModel realItemModel = dataList.get(firstViewIndex + 2);
                if (realItemModel.isEnd()) {
                    view_footer.setVisibility(Component.VISIBLE);
                    tv_view_footer.setText(realItemModel.getIndex() + "");
                    int headIndex;
                    if (Integer.parseInt(realItemModel.getTitle()) == 0) {
                        headIndex = 0;
                    } else {
                        headIndex = realItemModel.getIndex() + 1 - (realItemModel.getIndex() + 1) / (Integer.parseInt(realItemModel.getTitle()) + 1);
                    }
                    tv_view_header.setText(headIndex + "");
                    view_item.setVisibility(Component.VISIBLE);
                    tv_view_item.setText((realItemModel.getIndex() + 3) + "");
                } else if (!realItemModel.isEnd() && !realItemModel.isHeader()) {
                    view_item.setVisibility(Component.VISIBLE);
                    tv_view_item.setText(realItemModel.getIndex() + "");
                    ItemModel footModel = null;
                    for (int i = firstViewIndex + 2; i < (visibleViewCount + firstViewIndex + 2); i++) {
                        if (i >= dataList.size()) {
                            break;
                        }
                        ItemModel model = dataList.get(i);
                        if (model.isEnd()) {
                            view_footer.setVisibility(Component.VISIBLE);
                            tv_view_footer.setText(model.getIndex() + "");
                            footModel = model;
                            break;
                        }
                    }
                    if (footModel != null) {
                        int headIndex;
                        if (Integer.parseInt(footModel.getTitle()) == 0) {
                            headIndex = 0;
                        } else {
                            headIndex = footModel.getIndex() + 1 - (footModel.getIndex() + 1) / (Integer.parseInt(footModel.getTitle()) + 1);
                        }
                        tv_view_header.setText(headIndex + "");
                    } else {
                        for (int i = firstViewIndex + 2; i < (visibleViewCount + firstViewIndex + 2); i++) {
                            if (i >= dataList.size()) {
                                break;
                            }
                            ItemModel model = dataList.get(i);
                            if (model.isHeader()) {
                                tv_view_header.setText((Integer.parseInt(model.getTitle()) - 1) + "");
                                break;
                            }
                        }
                    }
                } else {
                    view_item.setVisibility(Component.VISIBLE);
                    tv_view_item.setText((realItemModel.getIndex() + 2) + "");
                    int headIndex;
                    if (Integer.parseInt(realItemModel.getTitle()) == 0) {
                        headIndex = 0;
                    } else {
                        headIndex = realItemModel.getIndex() - (realItemModel.getIndex()) / (Integer.parseInt(realItemModel.getTitle()) + 1);
                    }
                    tv_view_header.setText(headIndex + "");
                    for (int i = firstViewIndex + 2; i < (visibleViewCount + firstViewIndex + 2); i++) {
                        if (i >= dataList.size()) {
                            break;
                        }
                        ItemModel model = dataList.get(i);
                        if (model.isEnd()) {
                            view_footer.setVisibility(Component.VISIBLE);
                            tv_view_footer.setText(model.getIndex() + "");
                            break;
                        }
                    }
                }
            }
        } catch (NumberFormatException exception) {
            exception.fillInStackTrace();
        }

        ok.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                commonDialog.remove();
            }
        });

        commonDialog.setContentCustomComponent(component);
        commonDialog.setAutoClosable(true);
        commonDialog.setAlignment(LayoutAlignment.HORIZONTAL_CENTER);
        commonDialog.setSize(AttrHelper.vp2px(300, context), AttrHelper.vp2px(165, context));
        commonDialog.show();
    }

    public static void showReloadTool(Context context, ListContainer listContainer) {
        showReloadTool(context, listContainer, null);
    }

    public static void showReloadTool(Context context, ListContainer listContainer, IReload reload) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_reload, null, false);
        PopupDialog popupDialog = new PopupDialog(context, component, AttrHelper.vp2px(150, context), AttrHelper.vp2px(45, context));
        DependentLayout dl_reload = (DependentLayout) component.findComponentById(ResourceTable.Id_dl_reload);
        dl_reload.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                popupDialog.remove();
                if (reload != null) {
                    reload.reloadData();
                }
            }
        });

        popupDialog.setAutoClosable(true);
        popupDialog.setAlignment(LayoutAlignment.RIGHT | LayoutAlignment.TOP);
        popupDialog.setCustomComponent(component);
        popupDialog.setOffset(10, 100);
        popupDialog.setCornerRadius(10);
        popupDialog.show();
    }
}
